package cn.com.yang.algorithm.sort;

/**
 * 插入排序
 * @Author 杨亿金
 * @Date 2022/5/24 22:25
 * @CopyRight
 * @Version
 */
public class InsertionSort {

    public static void sort(Comparable[] arr){
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            for (int j = i; j >0 ; j--) {
                if (arr[j].compareTo(arr[j-1])<0){
                    swap(arr,j,j-1);
                }else{
                    break;
                }
            }
        }
    }

    private static void swap(Comparable[] arr, int j, int i) {
        Object t =arr[i];
        arr[i]=arr[j];
        arr[j]= (Comparable) t;
    }
}
